<#include "../include/header.html" />
    <script src="${base.contextPath}/common/code?taskType=TASK.TYPE" type="text/javascript"></script>
    <script type="text/javascript">
        var viewModel = Hap.createGridViewModel("#grid",{
            taskType:taskType,
        });
    </script>
    <body>
    <div id="taskEdit" ></div>
    <div id="groupEdit" ></div>
    <div id="assignEdit" ></div>
    <div id="page-content">
        <div class="pull-left" id="toolbar-btn" style="padding-bottom:10px;">
            <span class="btn btn-primary k-grid-add" style="float:left;margin-right:5px;" data-bind="click:create"><@spring.message "hap.new"/></span>
            <span class="btn btn-success k-grid-save-changes" data-bind="click:save" style="float:left;margin-right:5px;"><@spring.message "hap.save"/></span>
            <span  data-bind="click:remove" class="btn btn-danger" style="float:left;"><@spring.message "hap.delete"/></span>
        </div>
        <script>kendo.bind($('#toolbar-btn'), viewModel);</script>
        <div class="pull-right" id="query-form" style="padding-bottom:10px;">
            <input type="text" data-role="maskedtextbox" style="width:150px;margin-right:5px;" placeholder='<@spring.message "taskdetail.code"/>'
                   data-bind="value:model.code" class="k-textbox">
            <input type="text" data-role="maskedtextbox" style="width:150px;margin-right:5px;" placeholder='<@spring.message "taskdetail.name"/>'
                   data-bind="value:model.name" class="k-textbox">
            <select data-role="combobox" data-value-primitive="true" data-text-field="meaning" data-value-field="value"
                    data-bind="source: taskType,value:model.type" placeholder='<@spring.message "taskdetail.type"/>'
                    style="width: 150px;margin-right: 5px;"></select>
            <span class="btn btn-primary" style="width:70px" data-bind="click:query" type="submit"><@spring.message "hap.query"/></span>
            <div style="clear:both"></div>
        </div>
        <script>kendo.bind($('#query-form'), viewModel);</script>
        <div style="clear:both">
            <div id="grid"></div>
        </div>
    </div>

    <script type="text/javascript">
        Hap.initEnterQuery('#query-form', viewModel.query);

        var BaseUrl = _basePath;
        dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: BaseUrl + "/sys/task/detail/query",
                    type: "POST",
                    dataType: "json"
                },
                update: {
                    url: BaseUrl + "/sys/task/detail/submit",
                    type: "POST",
                    contentType: "application/json"
                },
                destroy: {
                    url: BaseUrl + "/sys/task/detail/remove",
                    type: "POST",
                    contentType: "application/json"
                },
                create: {
                    url: BaseUrl + "/sys/task/detail/submit",
                    type: "POST",
                    contentType: "application/json"
                },
                parameterMap: function (options, type) {
                    if (type !== "read" && options.models) {
                        var datas = Hap.prepareSubmitParameter(options, type);
                        return kendo.stringify(datas);
                    } else if (type === "read") {
                        return Hap.prepareQueryParameter(viewModel.model.toJSON(), options)
                    }
                }
            },
            batch: true,
            serverPaging: true,
            serverSorting: true,
            pageSize: 10,
            schema: {
                data: 'rows',
                total: 'total',
                model: {
                    id: "taskId",
                    fields: {
                        type:{defaultValue:"TASK"},
                        code:{validation: {required: true}},
                        name:{validation: {required: true}},
                        taskClass:{validation: {required: true}},
                        startDate: {type: "date",defaultValue:null},
                        endDate: {type: 'date',defaultValue:null},
                    },
                    editable:function (cell) {
                        if(cell == "taskClass" && this["type"] == "GROUP"){
                            return false;
                        }
                        return true;
                    },
                }
            }
        });

        var grid = $("#grid").kendoGrid({
            dataSource: dataSource,
            resizable: true,
            scrollable: true,
            navigatable: false,
            editable: true,
            sortable: true,
            selectable: 'multiple, rowbox',
            dataBound: function () {
                if (parent.autoResizeIframe) {
                    parent.autoResizeIframe('${RequestParameters.functionCode!}')
                }
            },
            pageable: {
                pageSizes: [5, 10, 20, 50],
                refresh: true,
                buttonCount: 5
            },
            columns: [
                {
                    field: "type",
                    title: '<@spring.message "taskdetail.type"/>',
                    width: 60,
                    headerAttributes: {
                        style  : "text-align: center"
                    },
                    attributes: {
                        style: "text-align: center"
                    },
                    template: function (dataItem) {
                        var v = dataItem.type ? dataItem.type : "";
                        $.each(taskType, function (i, n) {
                            if ((n.value || '').toLowerCase() == (v || '').toLowerCase()) {
                                v = n.meaning;
                                return v;
                            }
                        })
                        return v;
                    },
                    editable: function (rowdata) {
                        if(!!rowdata.id) {
                            return true;
                        }
                        return false;
                    },
                    editor: function (container, options) {
                        if(options.model.id != "") {
                            var v = options.model.type || "";
                            $.each(taskType, function (i, n) {
                                if ((n.value || '').toLowerCase() == (v || '').toLowerCase()) {
                                    v = n.meaning;
                                }
                            });
                            $("<span>" + v + "</span>").appendTo(container);

                        } else {
                            $('<input name="' + options.field + '"/>')
                                .appendTo(container)
                                .kendoDropDownList({
                                    dataTextField: "meaning",
                                    dataValueField: "value",
                                    valuePrimitive: true,
                                    dataSource: taskType,
                                    change: function (e) {
                                        var value = this.value();
                                        if (value == "GROUP") {
                                            options.model.taskClass = "";
                                        }
                                        grid.refresh();
                                    }
                                });
                        }
                    },
                },
                {
                    field: "code",
                    title: '<@spring.message "taskdetail.code"/>',
                    width: 60,

                },
                {
                    field: "name",
                    title: '<@spring.message "taskdetail.name"/>',
                    width: 80,
                    sortable: false,
                    editor: function (container, options) {
                        $('<input required name="' + options.field + '" />')
                            .appendTo(container)
                            .kendoTLEdit({
                                idField: 'taskId',
                                field: 'name',
                                dto: "io.choerodon.hap.task.dto.TaskDetail",
                                model: options.model
                            });
                    }

                },
                {
                    field: "taskClass",
                    title: '<@spring.message "taskdetail.taskclass"/>',
                    width: 120,
                    sortable: false,
//                    editor:function (container, options){
//                        return options.model.type==="TASK" ?
//                            $('<input name="'+options.field+'" data-role="maskedtextbox" class="k-textbox" />').appendTo(container):false;
//                    },
                },
                {
                    field: "description",
                    title: '<@spring.message "taskdetail.description"/>',
                    width: 120,
                    sortable: false,
                    editor: function (container, options) {
                        $('<input required name="' + options.field + '" />')
                            .appendTo(container)
                            .kendoTLEdit({
                                idField: 'taskId',
                                field: 'description',
                                dto: "io.choerodon.hap.task.dto.TaskDetail",
                                model: options.model
                            });
                    }
                },
                {
                    field: "startDate",
                    attributes: {style: "text-align:center"},
                    title: '<@spring.message "user.startActiveDate"/>',
                    width: 80,
                    headerAttributes: {style: "text-align: center"},
                    format: "{0:yyyy-MM-dd}",
                    editor: function (container, options) {
                        var end = options.model.endDate;
                        var opts = {
                            format: "yyyy-MM-dd"
                        };
                        if (end) {
                            opts.max = end;
                        }
                        $('<input name="' + options.field + '"/>')
                            .appendTo(container)
                            .kendoDatePicker(opts);
                    }
                },
                {
                    field: "endDate",
                    attributes: {style: "text-align:center"},
                    title: '<@spring.message "user.endActiveDate"/>',
                    width: 80,
                    headerAttributes: {style: "text-align: center"},
                    format: "{0:yyyy-MM-dd}",
                    editor: function (container, options) {
                        var start = options.model.startDate;
                        var opts = {
                            format: "yyyy-MM-dd"
                        };
                        if (start) {
                            opts.min = start;
                        }
                        $('<input name="' + options.field + '"/>')
                            .appendTo(container)
                            .kendoDatePicker(opts);
                    }
                },

                /* 操作 */
                {
                    title: '<@spring.message "hap.action"/>',
                    width: 60,
                    headerAttributes: {
                        style  : "text-align: center"
                    },
                    attributes: {
                        style: "text-align: center"
                    },
                    template: function (rowdata) {
                        if (!!rowdata.taskId) {
                            if (rowdata.type == "GROUP"){
                                return Hap.createAnchor('<@spring.message "taskdetail.editgroup"/>', groupEditWindow, rowdata.taskId);
                            }else {
                                return Hap.createAnchor('<@spring.message "taskdetail.edittask"/>', setParam, rowdata.taskId);
                            }
                        }
                        return '';
                    }
                },
                /* 授权 */
                {
                    title: '<@spring.message "taskdetail.assign"/>',
                    width: 60,
                    headerAttributes: {
                        style  : "text-align: center"
                    },
                    attributes: {
                        style: "text-align: center"
                    },
                    template: function (rowdata) {
                        if (!!rowdata.taskId) {
                            return Hap.createAnchor('<@spring.message "taskdetail.role.assign"/>', assignWindow, rowdata.taskId);
                        }
                        return '';
                    }
                }
            ],
        }).data("kendoGrid");


        function groupEditWindow(taskId ) {
            var url = '${base.contextPath}/task/task_group_edit.html';
            if(taskId){
                url = url + "?taskId="+taskId;
            }
            var editWin = Hap.createWindow('#groupEdit', {
                width: '85%',
                height: 500,
                title: '<@spring.message "hap.edit"/>',
                url: url
            });
            if (parent.autoResizeIframe) {
                parent.autoResizeIframe('${RequestParameters.functionCode!}', 600, function () {
                    editWin.center().open();
                })
            } else {
                editWin.center().open();
            }
        }
        
        function assignWindow(taskId) {
            var url = '${base.contextPath}/task/task_assign.html';
            if(taskId){
                url = url + "?taskId="+taskId;
            }
            var assignWin = Hap.createWindow('#assignEdit', {
                width: '90%',
                height: 530,
                title: '<@spring.message "hap.edit"/>',
                url: url
            })
            if (parent.autoResizeIframe) {
                parent.autoResizeIframe('${RequestParameters.functionCode!}', 630, function () {
                    assignWin.center().open();
                })
            } else {
                assignWin.center().open();
            }
        }

        function setParam(taskId){
            var  url =  '${base.contextPath}/sys/sys_parameter_config.html?code=TASK&targetId=' + taskId;
            var param = $("#taskEdit").kendoWindow({
                width: '90%',
                height: 550,
                title: '<@spring.message "hap.edit"/>',
                visible: false,
                iframe: true,
                resizable:false,
                modal: true,
                content:url
            }).data("kendoWindow");
            if(parent.autoResizeIframe){
                parent.autoResizeIframe('${RequestParameters.functionCode!}', 650, function(){
                    var win = $("#taskEdit").data("kendoWindow");
                    win.center().open();
                })
            }else {
                var win = $("#taskEdit").data("kendoWindow");
                win.center().open();
            }
        }


    </script>
    </body>
    </html>